package logic

import (
	"context"
	"fmt"
	"strings"

	"lottery-zero/external/award/lottery"
	"lottery-zero/external/utils"
	"lottery-zero/internal/svc"
	"lottery-zero/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type LottocheckLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLottocheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) LottocheckLogic {
	return LottocheckLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LottocheckLogic) Lottocheck(req types.LottoCheckReq) (*types.LottoCheckReply, error) {
	tick := lottery.Tick{Back: req.Back, Front: req.Front}
	retCheck := lottery.LotteryChecker.AwardCheck(&tick)
	var (
		AA []string
		BA []string
		CA []string
		DA []string
		EA []string
		FA []string
		GA []string
		HA []string
		IA []string
	)
	for _, as := range retCheck {
		ss := fmt.Sprintf("时间 %s, 期号 %s, 红球: %v, 蓝球 %s", as.T.Format("2006-01-02"), as.DrawNum, strings.Join(utils.BollIntToStrSlice(as.Front), "+"), strings.Join(utils.BollIntToStrSlice(as.Back), "+"))
		if as.A > 0 {
			AA = append(AA, ss)
		} else if as.B > 0 {
			BA = append(BA, ss)
		} else if as.C > 0 {
			CA = append(CA, ss)
		} else if as.D > 0 {
			DA = append(DA, ss)
		} else if as.E > 0 {
			EA = append(EA, ss)
		} else if as.F > 0 {
			FA = append(FA, ss)
		} else if as.G > 0 {
			GA = append(GA, ss)
		} else if as.H > 0 {
			HA = append(HA, ss)
		} else if as.I > 0 {
			IA = append(IA, ss)
		}
	}
	rets := []types.LottoAwardLevel{}
	if len(AA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "一等奖", Data: AA})
	}
	if len(BA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "二等奖", Data: BA})
	}
	if len(CA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "三等奖", Data: CA})
	}
	if len(DA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "四等奖", Data: DA})
	}
	if len(EA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "五等奖", Data: EA})
	}
	if len(FA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "六等奖", Data: FA})
	}
	if len(GA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "七等奖", Data: GA})
	}
	if len(HA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "八等奖", Data: HA})
	}
	if len(IA) > 0 {
		rets = append(rets, types.LottoAwardLevel{Title: "九等奖", Data: IA})
	}

	return &types.LottoCheckReply{Data: rets}, nil
}
